﻿using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RouteConvertTool
{
    /// <summary>
    /// 将航线转为Excel格式
    /// </summary>
    public class ExcelHelper
    {
        // 输出航点到 Excel 文件
        public static void ExportToExcel(List<WaypointX> waypoints, string fileName) 
        {
            // 确保文件路径有效
            string fullPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
            ExportToExcelFile(waypoints,fullPath);
        }


        // 输出航点到 Excel 文件
        public static void ExportToExcelFile(List<WaypointX> waypoints, string fullPath)
        {
           

            // 创建工作簿
            using (var workbook = new XLWorkbook())
            {
                // 添加工作表
                var worksheet = workbook.Worksheets.Add("Waypoints");

                // 定义列标题
                var columnHeaders = new Dictionary<string, int>
            {
                { "序号", 1 },
                { "线路名称", 2 },
                { "电压等级", 3 },
                { "杆塔编号", 4 },
                { "杆塔性质", 5 },
                { "相位", 6 },
                { "耐张塔侧", 7 },
                { "部件标签", 8 },
                { "相对位置", 9 },
                { "备注", 10 },
                { "云台角度", 11 },
                { "偏航角", 12 },
                { "经度", 13 },
                { "纬度", 14 },
                { "绝对高度", 15 },
                { "X", 16 },
                { "Y", 17 },
                { "组件经度", 18 },
                { "组件纬度", 19 },
                { "组件海拔", 20 },
                { "组件X", 21 },
                { "组件Y", 22 },
                { "起始高度", 23 }
            };

                // 写入列标题
                foreach (var header in columnHeaders)
                {
                    worksheet.Cell(1, header.Value).Value = header.Key;
                }

                // 写入航点数据
                int row = 2;
                foreach (var waypoint in waypoints)
                {
                    int column = 1;
                    worksheet.Cell(row, column++).Value = waypoint.SerialNo;
                    worksheet.Cell(row, column++).Value = waypoint.LineName;
                    worksheet.Cell(row, column++).Value = waypoint.VoltageLevel;
                    worksheet.Cell(row, column++).Value = waypoint.TowerNumber;
                    worksheet.Cell(row, column++).Value = waypoint.TowerType;
                    worksheet.Cell(row, column++).Value = waypoint.Phase;
                    worksheet.Cell(row, column++).Value = waypoint.StrainSide;
                    worksheet.Cell(row, column++).Value = waypoint.ComponentTag;
                    worksheet.Cell(row, column++).Value = waypoint.RelativePosition;
                    worksheet.Cell(row, column++).Value = waypoint.Notes;
                    worksheet.Cell(row, column++).Value = waypoint.GimbalPitchAngle;
                    worksheet.Cell(row, column++).Value = waypoint.YawAngle;
                    worksheet.Cell(row, column++).Value = "'" + waypoint.Longitude;
                    worksheet.Cell(row, column++).Value = "'" + waypoint.Latitude;
                    worksheet.Cell(row, column++).Value = waypoint.AbsoluteHeight;
                    worksheet.Cell(row, column++).Value = waypoint.X;
                    worksheet.Cell(row, column++).Value = waypoint.Y;
                    worksheet.Cell(row, column++).Value = "'" + waypoint.ComponentLongitude;
                    worksheet.Cell(row, column++).Value = "'" + waypoint.ComponentLatitude;
                    worksheet.Cell(row, column++).Value = waypoint.ComponentAltitude;
                    worksheet.Cell(row, column++).Value = waypoint.ComponentX;
                    worksheet.Cell(row, column++).Value = waypoint.ComponentY;
                    worksheet.Cell(row, column).Value = waypoint.StartHeight;

                    row++;
                }

                // 自动调整列宽
                worksheet.Columns().AdjustToContents(1,10,20);
                // 手动调整特定列的宽度（可选）
                worksheet.Column(2).Width = 20; // 相位列

                // 保存工作簿
                workbook.SaveAs(fullPath);
                Console.WriteLine($"成功生成文件: {fullPath}");
            }
        }
    }
}
